package activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {
	private ConnectionFactory connectionFactory=null;
	private Connection connection=null;
	private Session session=null;
	
	public Connection getConnection() {
		return connection;
	}
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	public Session getSession() {
		return session;
	}
	public void setSession(Session session) {
		this.session = session;
	}
	public Producer(){
		init();
	}
	private void init(){
		connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
				ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://0.0.0.0:6666");
		Connection connection=null;
		try {
			connection = connectionFactory.createConnection();			
			connection.start();		
			session=connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				connection.close();
			} catch (JMSException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	public static void runSimpleProducer(){

		Producer producer=new Producer();
		Session session=producer.getSession();
		Destination dest;
		try {
			dest = session.createQueue("zjj");
			Message msg=session.createMessage();
			msg.setStringProperty("zjj", "hello");
			MessageProducer msgProducer=session.createProducer(dest);
			while(true){
				Thread.sleep(5000);
				msgProducer.send(msg);
				System.out.println("send success");
			}		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(producer!=null){
				try {
					producer.getConnection().close();
				} catch (JMSException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}			
		}
	}
	public static void runConditionProducer(){
		Producer producer=new Producer();
		Session session=producer.getSession();
		Destination dest;
		try {
			dest = session.createQueue("condition");			
			MessageProducer msgProducer=session.createProducer(dest);
			int n=0;
			while(true){
				Thread.sleep(5000);
				Message msg=session.createMessage();
				msg.setIntProperty("number", n);
				msgProducer.send(msg);
				System.out.println("send success"+n);
				n++;
			}		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(producer!=null){
				try {
					producer.getConnection().close();
				} catch (JMSException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}			
		}
	}
	public static void main(String[] args) {
		runConditionProducer();
	}
}
